// pages/collageWare/collageWare.js
var WxParse = require('../../wxParse/wxParse.js');
// pages/collageWare/collageWare.js

var total_micro_second;

// 时间格式化输出，如3:25:19 86。每10ms都会调用一次
function dateformat(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = Math.floor((second - hr * 3600) / 60);
  // 秒位
  var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = Math.floor((micro_second % 1000) / 10);
  return hr + ":" + min + ":" + sec;
};
/* 毫秒级倒计时 */
function countdown(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: dateformat(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    countdown(that);
  }
    , 10)
}
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movies: [
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" }
    ],
    clock: '',
    num: 1,
    minusStatus: 'disable',
    _num: 1,
    display_evaluate: "none",
    display_collageWare: "block",
    display_details: "none",
    display_confirm: "none",

    collect: "inline-block",
    collect_select: "none",
    mySale: "none",
    sale: [
      {
        src: "../image/salebg_03.png",
        money: 10,
        use_way: "满100元可使用",
        name: "优惠券",
        time: "2018.05.23-2018.05.24",
        click_get: "点击领取",
        isSelect: false,
      },
      {
        src: "../image/salebg_03.png",
        money: 10,
        use_way: "满100元可使用",
        name: "优惠券",
        time: "2018.05.23-2018.05.24",
        click_get: "点击领取",
        isSelect: false,
      }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    countdown(this);
    //app.navTo('../confirm/confirm')
    wx.request({
      url: app.data.Url + 'spell/defaults',
      data: {
        sid: options.id
      },
      success: function (e) {
        that.setData({
          content: e.data.data,
          goods: e.data.data.attr
        })
        WxParse.wxParse("result", "html", e.data.data.content, that, 5)
        total_micro_second = e.data.data.createtime
        wx.request({
          url: app.data.Url + 'comment/commentLists',
          data: {
            goodsid: e.data.data.goodsid
          },
          method: 'get',
          success: function (res) {
            console.log('商品评论', res.data.data)
            if (res.data.data.length > 0) {
              that.setData({
                comment: res.data.data[0],
                comments: res.data.data
              })
            }

          }
        })
      }
    })
  
  },

  sale: function () {
    this.setData({
      mySale: "block",
      // add_confirm: "block",
    })
  },
  get_sale: function (e) {
    console.log(e.target.dataset.index)
    let index = e.target.dataset.index;
    this.data.sale[index].isSelect = !this.data.sale[index].isSelect;

    // src = this.data.sale[index].src
    if (this.data.sale[index].isSelect) {
      // this.setData({
      this.data.sale[index].src = "../image/has_get_03.png"
      // })
    }
    this.setData({
      sale: this.data.sale,
    })
  },
  bindMinus: function () {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  /*点击加号*/
  bindPlus: function (e) {
    console.log(e)
    var num = this.data.num;
    num++;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  /*输入框事件*/
  bindManual: function (e) {
    var num = e.detail.value;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  focus_click: function (e) {
    this.setData({
      _num: e.target.dataset.num
    });
    if (e.target.dataset.num == 1) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "block",
        display_details: "none"
      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_evaluate: "block",
        display_collageWare: "none",
        display_details: "none"
      })
    };
    if (e.target.dataset.num == 3) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "none",
        display_details: "block"
      })
    };
  },
  has_focus_click: function (e) {
    // console.log(e.target.dataset.number)
    var that = this;
    var type = that.data.goodstype;
    that.setData({
      _number: e.target.dataset.number
    })
    var data = this.data.goods;
    if (type == 1) {
      that.setData({
        shangpin: data,
        money: data[e.target.dataset.number].price,
        stock: data[e.target.dataset.number].stock,
        goodsname: data[e.target.dataset.number].rule,
        goodsid: data[e.target.dataset.number].goodsid,
        ruleid: data[e.target.dataset.number].attrid,
        goodstype: 1,
        _number: e.target.dataset.number,
      })
    } else {
      that.setData({
        shangpin: data,
        money: data[e.target.dataset.number].min_price,
        stock: data[e.target.dataset.number].stock,
        goodsname: data[e.target.dataset.number].rule,
        goodsid: data[e.target.dataset.number].goodsid,
        ruleid: data[e.target.dataset.number].attrid,
        goodstype: 2,
        _number: e.target.dataset.number,
      })
    }


  },
  // 增加数量
  addCount: function (e) {
    console.log(e)
    var num = this.data.num;
    num++;
    this.setData({
      num: num,
    })
  },
  // 减少数量
  minusCount: function (e) {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  buy_now: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;

    this.setData({
      display_confirm: "block",

    })
    if (id == 1) {
      var data = this.data.goods;
      that.setData({
        shangpin: data,
        money: data[0].price,
        goodsid: data[0].goodsid,
        ruleid: data[0].attrid,
        stock: data[0].stock,
        goodsname: data[0].rule,
        goodstype: 1,
        _number: 0,
      })
    } else {
      var data = this.data.goods;
      that.setData({
        shangpin: data,
        goodsid: data[0].goodsid,
        ruleid: data[0].attrid,
        money: data[0].min_price,
        stock: data[0].stock,
        goodsname: data[0].rule,
        goodstype: 2,
        _number: 0,
      })
    }

  },
  hideview: function () {
    this.setData({
      display_confirm: "none",
      mySale: "none",
    })
  },
  collect: function () {
    this.setData({
      collect: "none",
      collect_select: "inline-block"
    })
  },
  collect_select: function () {
    this.setData({
      collect: "inline-block",
      collect_select: "none"
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '快来和我拼团吧',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})